//  https://api.github.com/search/users?q=betterdamu
//  https://api.github.com/search.html
// 合理的使用url:  带静态资源文件后缀的url尽量不要定义成后台路由
const koa = require("koa");
const Router = require("@koa/router")
const bodyParser = require('koa-bodyparser');
const app = new koa();
const router = new Router({
    prefix:"/hello"
});


router.get('/:id', async (ctx) => {
    // console.log(ctx.query)
    console.log(ctx.params)
    ctx.body = 'get world';
});
router.post('/', async (ctx) => {
    console.log(ctx.request.body)
    ctx.body = 'post world';
});
router.put('/', async (ctx) => {
    console.log(ctx.headers.damu)
    ctx.body = 'put world';
});
router.patch('/', async (ctx) => {
    ctx.status = 404
    ctx.body = 'patch world';
});
// router.delete('/', async (ctx) => {
//     ctx.body = 'delete world';
// });

router.get('/', async (ctx,next) => {
    ctx.body = '多';
    await next()
},async (ctx,next)=>{
    ctx.body += '中';
    await next()
},async (ctx)=>{
    ctx.body += '间';
});



app.use(bodyParser()); //给ctx.request注册一个body属性
app.use(router.routes());
app.use(router.allowedMethods()) //支持所有接口的预请求



app.listen(8888,"127.0.0.1",()=>{
    console.log("server is running on http://127.0.0.1:8888")
})